var request = undefined;
var selectEquipment = {};
var lastChargingRecord = [];

$(document).ready(function() {
	showLoading();
	selectEquipment = window.parent.document.getElementById("selectEquipment");
	if (selectEquipment && selectEquipment.value) {
		$('title', parent.document).html(selectEquipment && selectEquipment.value + "状态");
		$('#equipmentName').html(selectEquipment && selectEquipment.value);
	} else {
		$('title', parent.document).html($('title').text());
		$('#equipmentName').html("单个充电状");
	}
	$('#charging').html("上次充电记录");
	$('#leisure').hide();
	$('#fault').hide();
	$('#chargingStatus').hide();
	$('#leisureStatus').hide();
	$('#faultStatus').hide();
	$('#disconnect').hide();
	$('#connect').hide();
	setBaseUrl();
	getRealTimeStatus();
	getLastChargingRecord(selectEquipment);
	setTimeout(hideLoading, 1000);
})

function getLastChargingRecord(selectEquipment){
	$.ajax({
		url : 'searchChargingRecord',
		type : 'post',
		dataType : 'json',
		data : {page : 1, rows : 1,equipmentName : selectEquipment.value},
		success : function(data){
			lastChargingRecord = data && data.chargingRecordList && data.chargingRecordList[0] || [];
		},
		error : function(XMLHttpRequest, textStatus, errorThrown){
			if (textStatus == "timeout") {
				getRealTimeStatus();
			} else if (textStatus != "abort") {
				getRealTimeStatus();
			}
		}
	})
}

function getRealTimeStatus() {
	var ajax = new createAjax();
	ajax.type = "post";
	ajax.dataType = "json";
	ajax.url = "realTimeStatus";
	ajax.timeout = 30000;
	ajax.success = function(data, textStatus) {
		var length = data.length;
		var equipmentName = selectEquipment && selectEquipment.value;
		$('#fault').show();
		$('#faultStatus').show();
		$('#disconnect').show();
		for (var i=0;i<length;i++) {
			if (data[i] && data[i].EquipmentName == equipmentName) {
				var tbody = $("#chargeTbody");
				var $tr = $('<tr>').appendTo(tbody);
				if (data[i].communicationState == "正常") {
					if (data[i].deviceState == "充电中") {
						$('#chargeTitle').html("充电详情");
						$('#charging').show();
						$('#leisure').hide();
						$('#fault').hide();
						$('#chargingStatus').show();
						$('#leisureStatus').hide();
						$('#faultStatus').hide();
						clearTable("chargeTbody");
						$("<td>").html(data[i].carNumber).appendTo($tr);
						$("<td>").html(data[i].cardNo).appendTo($tr);
						$("<td>").html(data[i].startChargingDTTM).appendTo($tr);
						$("<td>").html(data[i].realChargedDTTM).appendTo($tr);
						$("<td>").html(data[i].realChargedSOC).appendTo($tr);
						$("<td>").html(round(data[i].chargedElectricity,2)).appendTo($tr);
					} else {
						$('#chargeTitle').html("上次充电记录");
						$('#charging').hide();
						$('#leisure').show();
						$('#fault').hide();
						$('#chargingStatus').hide();
						$('#leisureStatus').show();
						$('#faultStatus').hide();
						if (lastChargingRecord.length > 0) {
							clearTable("chargeTbody");
							$("<td>").html(lastChargingRecord.carNumber).appendTo($tr);
							$("<td>").html(lastChargingRecord.cardNo).appendTo($tr);
							$("<td>").html(timeStamp2String(lastChargingRecord.startChargingDTTM)).appendTo($tr);
							$("<td>").html(formatTime(lastChargingRecord.totalChargingDTTM)).appendTo($tr);
							$("<td>").html(lastChargingRecord.totalSOC).appendTo($tr);
							$("<td>").html(lastChargingRecord.totalChargingKWH).appendTo($tr);
						}
					}
					if(data[i].MaxSingleVol && data[i].MaxSingleVol != "0"){
						$('#totalVol').html("400");
						$('#maxSingleVol').html(data[i].MaxSingleVol);
						$('#maxSingleVolIndex').html(data[i].MaxSingleVolIndex);
					}
					if(data[i].MinSingleVol && data[i].MinSingleVol != "0"){
						$('#totalVol').html("400");
						$('#minSingleVol').html(data[i].MinSingleVol);
						$('#minSingleVolIndex').html(data[i].MinSingleVolIndex);
					}
					if(data[i].MaxTemperature && data[i].MaxTemperature != "0"){
						$('#totalTemperature').html("64");
						$('#maxTemperature').html(data[i].MaxTemperature);
					}
					if(data[i].MinTemperature && data[i].MinTemperature != "0"){
						$('#totalTemperature').html("64");
						$('#minTemperature').html(data[i].MinTemperature);
					}
					$('#outputelectric').html(data[i].outputelectric);
					$('#outputVoltage').html(data[i].outputVoltage);
					$('#outputPower').html(round(data[i].outputVoltage * data[i].outputelectric/1000,2));
					$('#currentSOC').html(data[i].CurrentSOC);
					$('#chargedElectricity').html(round(data[i].chargedElectricity,2));
					$('#totalChargingDTTM').html(data[i].realChargedDTTM);
					$('#connect').show();
					$('#disconnect').hide();
				} else {
					$('#chargeTitle').html("上次充电记录");
					$('#charging').hide();
					$('#leisure').hide();
					$('#fault').show();
					$('#chargingStatus').hide();
					$('#leisureStatus').hide();
					$('#faultStatus').show();
					if (lastChargingRecord.length > 0) {
						clearTable("chargeTbody");
						$("<td>").html(lastChargingRecord.carNumber).appendTo($tr);
						$("<td>").html(lastChargingRecord.cardNo).appendTo($tr);
						$("<td>").html(timeStamp2String(lastChargingRecord.startChargingDTTM)).appendTo($tr);
						$("<td>").html(formatTime(lastChargingRecord.totalChargingDTTM)).appendTo($tr);
						$("<td>").html(lastChargingRecord.totalSOC).appendTo($tr);
						$("<td>").html(lastChargingRecord.totalChargingKWH).appendTo($tr);
					}
				}
			}
		}
		getRealTimeStatus();
	};
	ajax.error = function(XMLHttpRequest, textStatus, errorThrown) {
		if (textStatus == "timeout") {
			getRealTimeStatus();
		} else if (textStatus != "abort") {
			getRealTimeStatus();
		}
	};
	if (request) {
		request.abort();
	}
	request = startAjax(ajax);
	ajax = null;
}
